﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Ink;
using System.Windows.Input;
using System.Reflection;
using Omnia.Library.Attributes;
using System.Linq;
using Omnia.Library;
using Microsoft.Phone.Tasks;
using System.Collections.Generic;
using Omnia.DataModels;
using System.Collections.ObjectModel;
using System.Xml.Linq;

namespace Omnia.ViewModels
{
    public class AboutAndHelpViewModel : ViewModelBase
    {
        Assembly assembly = Assembly.Load("Omnia.Wallet");

        WebClient client = new WebClient();

        public AboutAndHelpViewModel()
        {
            client.DownloadStringCompleted -= client_DownloadStringCompleted;
            client.DownloadStringCompleted += client_DownloadStringCompleted;

            client.DownloadStringAsync(new Uri(string.Format("{0}{1}.xml", ConstKeys.HelpUrl, GlobalSettings.Language), UriKind.RelativeOrAbsolute));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                XDocument document = XDocument.Parse(e.Result);
                var items = (from element in document.Descendants("item")
                             select new HelpInfo()
                             {
                                 Id = Convert.ToInt32(element.Element("id").Value),
                                 Question = element.Element("question").Value,
                                 Answer = element.Element("answer").Value
                             });
                HelpCollection = new ObservableCollection<HelpInfo>(items);

            }
        }

        public Version Version
        {
            get
            {
                try
                {
                    AssemblyName name = assembly.GetName();
                    return name.Version;
                }
                catch
                {
                    String appVersion = assembly.FullName.Split('=')[1].Split(',')[0];
                    return Version.Parse(appVersion);
                }
            }
        }

        public DateTime ReleaseDate
        {
            get
            {
                var date = assembly.GetCustomAttributes(typeof(AssemblyReleaseDateAttribute), false).Cast<AssemblyReleaseDateAttribute>();
                return DateTime.Parse(date.FirstOrDefault().Value);
            }
        }

        public string Copyright
        {
            get
            {
                return assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false).Cast<AssemblyCopyrightAttribute>().FirstOrDefault().Copyright;
            }
        }

        public ICommand GotoMarketReviewCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        MarketplaceReviewTask task = new MarketplaceReviewTask();
                        task.Show();
                    }
                };
            }
        }

        public ICommand GotoMarketDetailsCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        MarketplaceDetailTask task = new MarketplaceDetailTask();
                        task.Show();
                    }
                };
            }
        }

        private ObservableCollection<HelpInfo> helpCollecion;
        public ObservableCollection<HelpInfo> HelpCollection
        {
            get { return helpCollecion; }
            set
            {
                helpCollecion = value;
                RaisePropertyChanged(() => HelpCollection);
            }
        }
    }
}
